﻿using System;
using System.IO;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.Mail;
using CE.User;
using CE.Global;
using CE.Security;
using CE.InputOut;
using CE.Database;
using CE.CarInsurance;
using CXDecryption;

public partial class carinsurance_fleet_FleetCompanyList : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            LoadGrid(1);
            txtCompanyId.Text = "0";
            if (Request.QueryString["cid"] != null)
            {
                txtCompanyId.Text = Request.QueryString["cid"].ToString();
                LoadGrid(2);
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtSearch.Text.Trim().Length > 5)
            {
                LoadGrid(3);
            }
            else if (txtCompanyId.Text != "0")
            {
                LoadGrid(2);
            }
            else
            {
                LoadGrid(1);
            }
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnNewCompany_Click(object sender, EventArgs e)
    {
        Response.Redirect("FleetCompany.aspx");
    }

    protected void grvFleetCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvFleetCompany.Rows.Count > 0)
        {
            Response.Redirect("FleetCompany.aspx?CompanyId=" + grvFleetCompany.SelectedDataKey.Value);
        }

    }

    protected void grvFleetCompany_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("MyDelete"))
        {
            if (e.CommandArgument != null)
            {
                Int64 CompanytId = 0;
                Int64 RefCEUserId = 0;

                Int64 _result = -1;
                CompanytId = Convert.ToInt64(e.CommandArgument);
                RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

                FleetCompanyDB _fleetcompanydb = new FleetCompanyDB();
                _result = _fleetcompanydb.Delete(CompanytId, RefCEUserId);

                if (_result > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Delete was successful.');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Delete failed.');", true);
                }
                LoadGrid(1);
            }
        }
        if (e.CommandName.Equals("MyCar"))
        {
            if (e.CommandArgument != null)
            {
                Response.Redirect("NewFleetCar.aspx?CompanyId=" + e.CommandArgument.ToString());
            }
        }
        if (e.CommandName.Equals("MyFile"))
        {
            if (e.CommandArgument != null)
            {
                Response.Redirect("FleetCarFiles.aspx?CompanyId=" + e.CommandArgument.ToString());
            }
        }
        if (e.CommandName.Equals("MyQuote"))
        {
            if (e.CommandArgument != null)
            {
                Response.Redirect("FleetManualQuote.aspx?CompanyId=" + e.CommandArgument.ToString());
            }
        }
    }

    private void LoadGrid(Int16 LaodId)
    {
        DataSet _dsFleetCompany = new DataSet();
        FleetCompanyDB _fleetcompanydb = new FleetCompanyDB();
        if (LaodId == 1)
        {
            _dsFleetCompany = _fleetcompanydb.GetFleetCompany();
        }
        else if (LaodId == 2)
        {
            _dsFleetCompany = _fleetcompanydb.GetFleetCompany(Convert.ToInt64(txtCompanyId.Text.Trim()));
        }
        else if (LaodId == 3)
        {
            _dsFleetCompany = _fleetcompanydb.GetFleetCompany(Server.HtmlEncode(txtSearch.Text.Trim()));
        }
        else if (LaodId == 4)
        {
            _dsFleetCompany = _fleetcompanydb.GetFleetCompany(Convert.ToInt64(txtCompanyId.Text.Trim()),Server.HtmlEncode(txtSearch.Text.Trim()));
        }
        if (_dsFleetCompany != null && _dsFleetCompany.Tables.Count > 0)
        {
            grvFleetCompany.DataSource = _dsFleetCompany.Tables[0];
            grvFleetCompany.DataBind();
        }
        else
        {
            grvFleetCompany.DataSource = null;
            grvFleetCompany.DataBind();
        }
    }

}
